package cn.javaweb.model;

import cn.javaweb.entity.User;
import cn.javaweb.library.Database;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BaseModel {
    protected Connection connection = null;

    public BaseModel(){
        this.connection = Database.getDatabaseConnection();
    }

    public Object parseObject(ResultSet rs){
        return null;

    }

    public List<Object> selectAll(String sql, Object[] params) {

        ArrayList<Object> list = new ArrayList<Object>();

        PreparedStatement pst = null;
        try{
            pst = connection.prepareStatement(sql);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("BaseModel.selectAll1"+e.getMessage());
            return null;
        }

        ResultSet rs = null;

        try{
            if(params!=null){
                for(int i=0; i<params.length; i++){
                    pst.setObject(i+1, params[i]);
                }
            }
            rs = pst.executeQuery();

            while(rs.next()){
                Object a = this.parseObject(rs);
                list.add(this.parseObject(rs));
            }
            rs.close();
            pst.close();
            return list;
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("BaseModel.selectAll2"+e.getMessage());
            return null;
        }
    }

    public Object selectOne(String sql, Object[] params){
        ArrayList<Object> list = new ArrayList<Object>();
        PreparedStatement pst = null;
        try{
            pst = connection.prepareStatement(sql);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("BaseModel.selectOne1:"+e.getMessage());
            return null;
        }

        ResultSet rs = null;

        try{
            if(params!=null){
                for(int i=0; i<params.length; i++){
                    pst.setObject(i+1, params[i]);
                }
            }
            rs = pst.executeQuery();
            if(rs.next()){
                Object ret = this.parseObject(rs);
                rs.close();
                pst.close();
                return ret;
            }
            return null;
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("BaseModel.selectOne2:"+e.getMessage());
            return null;
        }
    }


    public int getCount(String sql, Object[] params){
        int ret = 0;
        PreparedStatement pst = null;
        try{
            pst = connection.prepareStatement(sql);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
        ResultSet rs = null;
        try{
            if(params!=null){
                for(int i=0; i<params.length; i++){
                    pst.setObject(i+1, params[i]);
                }
            }
            rs = pst.executeQuery();
            if(rs.next()){
                ret = rs.getInt("cnt");
            }
            rs.close();
            pst.close();
            return ret;
        }catch(Exception e){
            e.printStackTrace();
            System.out.println("BaseModel.getCount:"+e.getMessage());
        }
        return ret;


    }

    public int update(String sql, Object[] params){
        int affectedLine = 0;
        try{
            PreparedStatement pst = null;
            try{
                pst = connection.prepareStatement(sql);
            }catch (Exception e){
                e.printStackTrace();
                return 0;
            }

            ResultSet rs = null;
            if(params!=null){
                for(int i=0; i<params.length; i++){
                    pst.setObject(i+1, params[i]);
                }
            }
            affectedLine = pst.executeUpdate();

        }catch(Exception e){
            e.printStackTrace();
            System.out.println("BaseModel.update:"+e.getMessage());
        }


        return affectedLine;
    }


}
